package com.dhxy.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.dhxy.entity.Average_user;
import com.dhxy.entity.User;


public class UserDao extends DataBaseDao {
	public List<User> chaxun() throws ClassNotFoundException, SQLException {// 查找所有用户信息方法
		load();
		List<User> arrayList = new ArrayList();// 用户信息存到集合
		ResultSet rs = stmt.executeQuery("SELECT * FROM user;");// mysql语句
		User user = null;// 定义User变量
		while (rs.next()) {
			user = new User();
			user.setUnumber(rs.getInt("unumber"));
			user.setUname(rs.getString("uname"));
			user.setUpassword(rs.getString("upassword"));
			user.setUphonenumber(rs.getInt("uphonenumber"));
			user.setAddress(rs.getString("address"));
			user.setIntegral(rs.getInt("integral"));
			arrayList.add(user);
		}
		closeAll();
		return arrayList;// 放回ArrayList集合

	}

	public int inquiries(String uname, String upassword) throws ClassNotFoundException, SQLException {
		load();
		int count = 0;
		List<Average_user> arrayList = new ArrayList();
		ResultSet re = stmt
				.executeQuery("SELECT count(*) from uname where uname=" + uname + " and pwd=" + upassword + "");
		Average_user ae_user = new Average_user();
		if (re.next()) {
			count = re.getInt(1);
			System.out.println(count);
		}
		closeAll();
		return count;

	}

	public User number(int number) throws ClassNotFoundException, SQLException {
		load();
		ResultSet rs = stmt.executeQuery("SELECT unumber FROM User;");// mysql语句
		User user = new User();
		while (rs.next()) {
			user.setUnumber(rs.getInt("unumber"));
			user.setUname(rs.getString("uname"));
			user.setUpassword(rs.getString("upassword"));
			user.setUphonenumber(rs.getInt("uphonenumber"));
			user.setAddress(rs.getString("address"));
			user.setIntegral(rs.getInt("integral"));
		}
		closeAll();
		return user;

	}

	public int answerUp(User user) throws ClassNotFoundException, SQLException {
		String uname = user.getUname();
		String upassword = user.getUpassword();
		int uphonenumber = user.getUphonenumber();
		String address = user.getAddress();
		int integral = user.getIntegral();
		String str = "INSERT INTO Userinfo(uname,upassword,uphonenumber,address) VALUES(?,?,?,?,?);";
		return super.addAll(str, uname, upassword, uphonenumber, address, integral);
	}

	public int getVarByUname(String Uname) throws ClassNotFoundException, SQLException {
		load();
		int count = 0;
		ResultSet re = stmt.executeQuery("SELECT count(*) from uname where uname=" + Uname + "");
		if (re.next()) {
			count = re.getInt(1);
		}
		closeAll();
		return count;
	}
}
